// use clap::Parser;
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize)]
pub struct Msg {
    pub msg_type: MsgType,

    pub nick_name: String,
    pub data: String,
}

impl Msg {
    pub fn new(value: &str) -> Self {
        Msg {
            msg_type: MsgType::CHAT,
            data: value.replace("\n", "").to_string(),
            nick_name: "UNKNOWN".to_string(),
        }
    }


    pub fn sign_up(nick_name: &str) -> Self {
        let nick_name = nick_name.to_string().replace("\n", "");
        Msg {
            msg_type: MsgType::SIGN_UP,
            data: nick_name,
            nick_name: "UNKNOWN".to_string(),
        }
    }


    pub fn quit() -> Self {
        Msg {
            msg_type: MsgType::QUIT,
            data: "UNKNOWN".to_string(),
            nick_name: "UNKNOWN".to_string(),
        }
    }

    pub fn quit2(nick_name : String) -> Self {
        Msg {
            msg_type: MsgType::QUIT,
            data: "UNKNOWN".to_string(),
            nick_name: nick_name,
        }
    }

    pub fn sign_up_success() -> Self {
        Msg {
            msg_type: MsgType::SIGN_UP_SUCCESS,
            data: "".to_string(),
            nick_name: "UNKNOWN".to_string(),
        }
    }


    pub fn sign_up_broadcast(nick_name : String) -> Self {
        Msg {
            msg_type: MsgType::SIGN_UP_BROADCAST,
            data: "".to_string(),
            nick_name: nick_name,
        }
    }


    pub fn sign_up_fail(value: &str) -> Self {
        Msg {
            msg_type: MsgType::SIGN_UP_FAIL,
            data: value.to_string(),
            nick_name: "UNKNOWN".to_string(),
        }
    }

    pub fn to_string(&self) -> String {
        let mut json = serde_json::to_string(self).unwrap();
        json.push_str("\n");
        json
    }

    pub fn parse(value: &str) -> Self {
        // println!("value is {}", value);
        let msg_obj: Msg = serde_json::from_str(value).unwrap();
        msg_obj
    }
}


#[derive(Debug, Deserialize, Serialize)]
pub enum MsgType {
    SIGN_UP,
    SIGN_UP_BROADCAST,
    SIGN_UP_SUCCESS,
    SIGN_UP_FAIL,
    CHAT,
    #[serde(rename = ":quit")]
    QUIT,
    QUIT_BROADCAST,
    NOT_SUPPORT,
}


// impl From<&str> for MsgType {
//     fn from(value: &str) -> Self {
//         match value {
//             "SIGN_UP" => MsgType::SIGN_UP,
//
//             "CHAT" => MsgType::CHAT,
//             ":QUIT" => MsgType::QUIT,
//             _ => MsgType::NOT_SUPPORT,
//         }
//     }
// }